﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SmartVault.Proto
{
    /// <summary>
    /// EffectivePermissionFlags
    /// 
    /// These represent the permissions in NodeResponse.message.your_permissions.
    /// These represent what a specific user can effectively do. This takes into
    /// account several things, including:
    /// 
    /// A. Access Control List
    /// B. License Type
    /// C. Capabilities of Node (i.e. you can't create files under an account)
    /// </summary>
    public class EffectivePermissionFlags
    {
        public const int Write = 0x01;
        public const int CreateFolders = 0x04;
        public const int Copy = 0x08;
        public const int Delete = 0x10;
        public const int ManageSecurity = 0x20;
        public const int Read = 0x40;
        public const int CreateCompany = 0x1000;
        public const int CreateFiles = 0x2000;

        public static bool CanWrite(Int32 perm)
        {
            return (perm & EffectivePermissionFlags.Write) == EffectivePermissionFlags.Write;                    
        }

        public static bool CanCreateFiles(Int32 perm)
        {
            return (perm & EffectivePermissionFlags.CreateFiles) == EffectivePermissionFlags.CreateFiles;                    
        }

        public static bool CanCreateFolders(Int32 perm)
        {
            return (perm & EffectivePermissionFlags.CreateFolders) == EffectivePermissionFlags.CreateFolders;                    
        }

        public static bool CanCopy(Int32 perm)
        {
            return (perm & EffectivePermissionFlags.Copy) == EffectivePermissionFlags.Copy;                    
        }

        public static bool CanDelete(Int32 perm)
        {
            return (perm & EffectivePermissionFlags.Delete) == EffectivePermissionFlags.Delete;
        }

        public static bool CanManageSecurity(Int32 perm)
        {
            return (perm & EffectivePermissionFlags.ManageSecurity) == EffectivePermissionFlags.ManageSecurity;
        }

        public static  bool CanRead(Int32 perm)
        {
            return (perm & EffectivePermissionFlags.Read) == EffectivePermissionFlags.Read;
        }

        public static bool CanCreateCompany(Int32 perm)
        {
            return (perm & EffectivePermissionFlags.CreateCompany) == EffectivePermissionFlags.CreateCompany;
        }

        private static StringBuilder AddPerm(bool can, StringBuilder bld, string name)
        {
            if (can)
            {
                if (bld.Length != 0)
                {
                    bld.Append(",");
                }
                bld.Append(name);
            }

            return bld;
        }

        public static string GetDisplayString(Int32 perm)
        {
            StringBuilder bld = new StringBuilder();
            AddPerm(CanWrite(perm), bld, "Write");
            AddPerm(CanCreateFiles(perm), bld, "Create Files");
            AddPerm(CanCreateFolders(perm), bld, "Create Folders");
            AddPerm(CanCopy(perm), bld, "Copy");
            AddPerm(CanDelete(perm), bld, "Delete");
            AddPerm(CanManageSecurity(perm), bld, "Manage Security");
            AddPerm(CanRead(perm), bld, "Read");
            AddPerm(CanCreateCompany(perm), bld, "Create Company");

            return "[" + bld.ToString() + "]";
        }
    };
}
